var wjtips = function (content) {
    layer.open({
        content: content,
        skin: 'msg',
        time: 3 //2秒后自动关闭
    });
};

var size = $(".member_nav li").size();
if (size > 0) {
    $(".member_nav li").each(function () {
        $(this).css("width", 100 / size + "%");
    })
}

var ajax_post = function (obj, parme, action, falseCallback, loadingtips, type) {
    loadingtips = loadingtips || '数据处理中...';
    layer.closeAll();
    layer.open({
        type: 2,
        content: loadingtips
    });
    setTimeout(function () {
        if (parme && action) {
            $.post(action, parme, success, "json");
        }
        else {
            $.post(obj.attr("action"), obj.serialize(), success, "json");
        }
    }, 1000);
    return false;

    function success(d) {
        // 2 => 登录
        if (d.code == 1) {
            layer.closeAll();
            wjtips(d.msg);
            var refer = type === 2 ? d.data.redirect_url || d.url : d.url || d.data;
            if (refer) {
                setTimeout(function () {
                    window.location.href = refer;
                }, 1500);
            } else {
                setTimeout("location.reload()", 1500);
            }
        } else {
            layer.closeAll();
            if (type == 2) {
                if (d.msg) {
                    wjtips(d.msg);
                    setTimeout("location.reload()", 1500);
                    return false;
                } else {
                    for (var i in d.data) {
                        wjtips(d.data[i]);
                        return false;
                    }
                }
            } else {
                wjtips(d.msg);
            }
            if (falseCallback == 1) {
                setTimeout("location.reload()", 1500);
            }
            return false;
        }
    }
};

var movetop = function (target) {
    target = $('#' + target);
    target.removeClass('hide').removeClass('mtb').addClass('mtt');
};

var movebot = function (target) {
    target = $('#' + target);
    target.removeClass('hide').removeClass('mtt').addClass('mtb');
    setTimeout(function () {
        target.addClass('hide');
    }, 400);
};

// 去掉转义字符、特殊字符
var clearOther = function (str) {
    var str1 = str.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
    var str2 = str1.replace(/[\@\+\.\,\!\|\~\`\(\)\#\$\%\^\&\*\{\}\:\;\"\L\<\\\>\?]/g, '');
    var str3 = str2.replace(/\[|]/g, '');
    return str3;
};

var getCookie = function (name) {
    return $.cookie(name);
};

var to_login = function (t) {
    var uid = isNaN($("#UID").val()) ? 0 : parseInt($("#UID").val());
    if (uid <= 0) {
        window.location.href = "/uc/sign/login.html";
    } else {
        window.location.href = $(t).attr("data-href");
    }
};

// 设置商家信息
var setPartnerInfo = function () {
    var partner = getCookie("partner");
    var flag = getCookie("flag");
    var partner2 = partner !== "undefined" ? clearOther(partner) : "";
    var flag2 = partner !== "undefined" ? clearOther(flag) : "";
    $("input[name='partner']").val(partner2);
    $("input[name='flag']").val(flag2);
};

var fontSize = function () {
    var width = $(window).width();
    $("html").css("font-size", width / 20);
};

//协议规则
var clickAgree = function (id) {
    $("#agree").click(function () {
        var status = $(this).prop("checked");
        if (status === true) {
            $(id).removeClass("disabled").prop("disabled", false);
        } else {
            $(id).addClass("disabled").prop("disabled", true);
        }
        $(this).val(status === true ? 1 : 0);
    });
};

// 银行存管提示关闭
var closeBankPopup = function (e) {
    var url = $(e).find(".closeBtn").attr("data-href");
    var condition = $(e).find(".closeBtn").attr("data-status");
    condition === 'true' ? $(e).fadeOut("fast") : (url ? window.location.href = url : window.location.reload());
};

var openBankPopup = function (e, t) {
    $(e).fadeIn("fast").find(".closeBtn").attr("data-status", t === true ? true : '');
};

var switch_user = function () {
    layer.open({
        content: '您当前是投资人身份，无法进行借款，确认要切换成借款人吗？'
        , btn: ['确定', '取消']
        , yes: function () {
            layer.closeAll();
            layer.open({type: 2, content: '切换中...'});
            $.post("/uc/index/switch_user.html", {}, function (d) {
                layer.closeAll();
                if (d.code === 1) {
                    var refer = d.data || d.url;
                    wjtips(d.msg);
                    setTimeout(function () {
                        window.location.href = refer;
                    }, 2000);
                } else {
                    wjtips(d.msg);
                    if (d.url) {
                        setTimeout(function () {
                            window.location.href = d.url;
                        }, 2000);
                    }
                }
                return false;
            }, "json");
        }
    });
};


/**
 * 延迟并刷新
 * */
var delayload = function (wait) {
    var url = window.location.href, new_url;
    var condition = url.indexOf("?wait=");
    if (condition > 0) {
        wait = url.substr(condition + 6, 10) || 3;
        new_url = url.split(url.substr(url.indexOf("?wait="), 10)).join("");
        layer.open({type: 2, content: '银行数据异步返回中...'});
        setTimeout(function () {
            window.location.href = new_url
        }, wait * 1000);
    } else {
        wait = null;
    }
};


$(function () {

    fontSize();
    $(window).resize(function () {
        fontSize();
    });
    delayload();
});